package com.ruoyi.web.controller.exam;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ObjectUtil;
import com.ruoyi.common.vo.PageResult;
import com.ruoyi.questionBank.domain.ExamPaper;
import com.ruoyi.questionBank.service.IExamPaperService;
import com.ruoyi.questionBank.vo.ExamPaperListVO;
import com.ruoyi.questionBank.vo.InsertQuestionVO;
import com.ruoyi.web.controller.exam.vo.PaperQuestionRemoveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/exam/paper")
public class ExamPaperController extends BaseController {
    @Autowired
    private IExamPaperService paperService;

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('exam:paper:list')")
    public TableDataInfo list(ExamPaperListVO vo){
        PageResult<ExamPaper> result = paperService.list(vo);
        return getDataTable(result.convertToPage());
    }

    @GetMapping("/info")
    @PreAuthorize("@ss.hasPermi('exam:paper:list')")
    public AjaxResult info(int id){
        ExamPaper paper = paperService.get(id);
        return AjaxResult.success(paper);
    }

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('exam:paper:add')")
    public AjaxResult add(@RequestBody ExamPaper examPaper){
        paperService.add(examPaper);
        return AjaxResult.success();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('exam:paper:update')")
    public AjaxResult update(@RequestBody ExamPaper examPaper){
        paperService.update(examPaper);
        return AjaxResult.success();
    }

    @PostMapping("/del")
    @PreAuthorize("@ss.hasPermi('exam:paper:del')")
    public AjaxResult del(@RequestBody int id){
        paperService.del(id);
        return AjaxResult.success();
    }

    @GetMapping("/all")
    @PreAuthorize("@ss.hasPermi('exam:paper:list')")
    public AjaxResult all(){
        ExamPaperListVO vo = new ExamPaperListVO();
        vo.setPageSize(Integer.MAX_VALUE);
        PageResult<ExamPaper> result = paperService.list(vo);
        return AjaxResult.success(result.getData());
    }

    @PostMapping("/insertQuestion")
    @PreAuthorize("@ss.hasPermi('exam:paper:update')")
    public AjaxResult insertQuestion(@RequestBody InsertQuestionVO vo){
        paperService.insertQuestion(vo);
        return AjaxResult.success();
    }

    @PostMapping("/removeQuestion")
    @PreAuthorize("@ss.hasPermi('exam:paper:update')")
    public AjaxResult removeQuestion(@RequestBody PaperQuestionRemoveVO removeVO){
        ObjectUtil.checkNull(removeVO.getId() ,"paper id");
        if(removeVO.getQuestionIds() != null){
            paperService.removeQuestion(removeVO.getId() ,removeVO.getQuestionIds());
        }else if(removeVO.getQid() != null){
            paperService.removeQuestion(removeVO.getId() ,removeVO.getQid());
        }else {
            throw new IllegalArgumentException("question id cannot be null");
        }
        return AjaxResult.success();
    }
}
